//
//  ADBannerView.m
//  RainbowFM
//
//  Created by chenli on 14/12/28.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "ADBannerView.h"
#import "UtilsCell.h"

@interface ADBannerView(){
    NSInteger currentIdex;
}

@end

@implementation ADBannerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)show{
    currentIdex = 0;
    _pageControl.numberOfPages = _listArray.count;
    _pageControl.currentPage = currentIdex;
    [self.collectionView reloadData];
    if (_scorllAnimation) {
        [NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(startScorll) userInfo:self repeats:YES];

    }
}

-(void)startScorll{
    currentIdex += 1;
    if (currentIdex >= _listArray.count) {
        currentIdex = 0;
    }
    
    _pageControl.currentPage = currentIdex;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:currentIdex inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _listArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ADCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ADCollectionCell" forIndexPath:indexPath];
    [cell.cellImgeView sd_setImageWithURL:[NSURL URLWithString:_listArray[indexPath.row]]];
    return cell;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_delegate && [_delegate respondsToSelector:@selector(adBannerSelectedIndex:)]) {
        [_delegate adBannerSelectedIndex:indexPath.row];
    }
}

@end
